package com.android.assignment.taskreminder;

import android.content.Context;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.database.Cursor;
import android.util.Log;
import java.util.Calendar;


public class OnBootReceiver extends BroadcastReceiver{
	private static final String TAG = OnBootReceiver.class.getCanonicalName();
	
	@Override
	public void onReceive(Context context, Intent intent){
		ReminderManager reminderMgr = new ReminderManager(context);
		Cursor cursor = context.getContentResolver().query(TaskReminderProvider.CONTENT_URI, null, null, null, null);
		if(cursor != null){
			cursor.moveToFirst();
			int rowIdColumnIndex = cursor.getColumnIndex(TaskReminderProvider.COLUMN_ROWID);
			int dateTimeColumIndex = cursor.getColumnIndex(TaskReminderProvider.COLUMN_DATE_TIME);
			
			while(cursor.isAfterLast()==false){
				long rowId =  cursor.getLong(rowIdColumnIndex);
				long dateTime = cursor.getLong(dateTimeColumIndex);
				Log.d(TAG,"adding alarm on boot...");
				Log.d(TAG,"Row id - " + rowId);
				Log.d(TAG,"Date Time - " + dateTime);
				
				Calendar cal = Calendar.getInstance();
				cal.setTime(new java.util.Date(dateTime));
				reminderMgr.setReminder(rowId, cal);
				
				cursor.moveToNext();
			}
			cursor.close();
		}
	}
}